<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 14:22
 */

namespace Shop\Api;


use Common\Api\Api;
use Shop\Model\WalletApplyModel;

class WalletApplyApi extends Api{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "Shop/WalletApply/queryNoPaging";
    /**
     * 查询，分页
     */
    const QUERY = "Shop/WalletApply/query";
    /**
     * 查询，分页带数量
     */
    const QUERY_WITH_COUNT = 'Shop/WalletApply/queryWithCount';

    /**
     * 添加
     */
    const ADD = "Shop/WalletApply/add";
    /**
     * 保存
     */
    const SAVE = "Shop/WalletApply/save";
    /**
     * 获取信息
     */
    const GET_INFO = "Shop/WalletApply/getInfo";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/WalletApply/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/WalletApply/delete";

    const PASS_WALLETAPPLY="Shop/WalletApply/passWalletApply";

    protected function _init(){
        $this->model = new WalletApplyModel();
    }


    /**
     * 审核通过
     * 1余额减少 + 记录
     * 2更改记录状态
     * 3信息谁送
     */
    public function passWalletApply($id,$uid){

        $trans = M();
        $trans->startTrans(); //开启事务
        $res = $this->getInfo(array('id'=>$id));
        if(!$res['status']) return $this->apiReturnErr($res['info']);
        if(empty($res['info'])) return $this->apiReturnErr('提现id错误');
        $wallet_out = (float)$res['info']['money'];
        $result = $this->saveByID($id,array('valid_status'=>WalletApplyModel::PASS));
        if($result['status']){
            if((int)$result['info']<0) return $this->apiReturnErr('未知错误');
            $result = apiCall(WalletApi::GET_INFO_If_NOT_EXIST_THEN_ADD,array($uid));
            if(!$result['status']){
                $trans->rollback();return $this->apiReturnErr($result['info']);
            }
            $array = $result['info'];
            if((int)$array['wallet_type'] === 1) {
                $trans->rollback();return $this->apiReturnErr('账号被冻结');
            }
            if($wallet_out <=0 ){
                $trans->rollback();return $this->apiReturnErr('提现金额错误');
            }
            $before_use_money = (float)$array['account_balance'] - (float)$array['frozen_funds'];//可用余额
            if($before_use_money < $wallet_out){
                $trans->rollback();
                return $this->apiReturnErr('可用余额不足');
            }
            $result = apiCall(WalletApi::SAVE_BY_ID,array($array['id'],array('account_balance'=>(float)$array['account_balance'] - $wallet_out)));
            if($result['status']){
                $map = array(
                'uid'          =>$uid,
                'before_money' =>$before_use_money,
                'plus'         =>0,
                'minus'        =>$wallet_out,
                'after_money'  =>$before_use_money - $wallet_out,
                'create_time'  =>time(),
                'dtree_type'   =>getDatatree('WALLET_WITHDRAW'),
                'reason'       =>'提现',
                );
                $result = apiCall(WalletHisApi::ADD,array($map));
                if($result['status']){
                    //推送消息
                    $msg = '您的提现已通过,请留意支付宝信息';
            $entity = array(
              'from_id' =>0,
              'title'   =>'提现申请变动',
              'content' =>$msg,
              'summary' =>$msg,
              'extra'   =>'', //消息记录中的
            );
            $after_open = array('type'=>'go_activity','param'=>MessageModel::MESSAGE_SYSTEM_ACTIVITY,'extra'=>array()); //推送参数
            $r = apiCall(MessageApi::PUSH_MESSAGE_WITH_TYPE,array(MessageModel::MESSAGE_SYSTEM,$entity,$uid,false,$after_open));
                    $trans->commit();//提交事务
                    if(!$r['status']) return $this->apiReturnSuc($r['info']);
                    return  $this->apiReturnSuc('提现成功，请确保外部已打款');
                }else{
                    $trans->rollback();//回滚事务
                    return  $result;
                }
            }else{
                $trans->rollback();//回滚事务
                return  $result;
            }

        }else{
            $trans->rollback();//回滚事务
            return $result;
        }

    }

}